import axios, {AxiosRequestConfig, AxiosResponse } from './axios'
// import { AxiosRequestConfig } from './axios/types'

const baseUrl = 'http://localhost:8080/'

interface User  {
  name: string,
  password: string
}
let user: User = {
  name: 'zhangsan',
  password: '123456'
}

// axios.interceptors.request.use((config: AxiosRequestConfig)=>{
//   config.headers!.name += '1'
//   return config;
// })
// axios.interceptors.request.use((config: AxiosRequestConfig)=>{
//   config.headers!.name += '2'
//   return config;
// })
// axios.interceptors.response.use((response: AxiosResponse)=>{
//   response.data.name += '1'
//   return response;
// })
// axios.interceptors.response.use((response: AxiosResponse)=>{
//   response.data.name += '2'
//   return response;
// })

const CancelToken = axios.CancelToken;
const source = CancelToken.source();
// axios是一个函数
axios({  // User修饰返回体data
  method: 'post',
  url: baseUrl+'post',
  timeout: 1000,
  params: user,
  cancelToken: source.token,
  headers: { 
    'Content-Type': 'application/json',
    'name': 'name'
  },
  data: user,
})
.then((res) => {
  console.log(res)
  return res.data
}).then(data=>{
}).catch(err=>{
  if(axios.isCancel(err)){
    console.log('请求取消', err);
  }else {
    console.log(err);
  }
})
source.cancel('取消请求')
  